var $safe = {
    v: {
        ajaxOption: {method: 'get', dataType: 'json', async: true},
        notifyMethod: null,
        dataTableL: {
            "sProcessing": "处理中...",
            "sLengthMenu": "显示 _MENU_ 项结果",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
            "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "表中数据为空",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        }
    },
    notify: function (msg, status) {
        var option = {
            position: "top center",
            autoHideDelay: 2000,
            className: status,
            arrowSize: 10
        };
        $.notify(msg, option);
    },
    uiform: function () {
        jQuery('tbody input:checkbox').click(function () {
            if (jQuery(this).is(':checked')) {
                jQuery(this).parent().addClass('checked');
                jQuery(this).parents('tr').addClass('warning');
            } else {
                jQuery(this).parent().removeClass('checked');
                jQuery(this).parents('tr').removeClass('warning');
            }
        });
    },
    cutText: function (sub, length, less) {
        var str = "";
        if (sub && sub.length > length) {
            str = sub.substr(0, length);
            if (less) {
                str = str + less;
                str = '<p title="' + sub + '">' + str + '</p>';
            }
        } else {
            str = sub;
        }

        return str;
    },
    checkAll: function (obj) {
        var parentTable = jQuery(obj).parents('table');
        var ch = parentTable.find('tbody input[type=checkbox]');
        if (jQuery(obj).is(':checked')) {
            ch.each(function () {
                jQuery(this).prop('checked', true);
                jQuery(this).parent().addClass('checked');
                jQuery(this).parents('tr').addClass('warning');
            });
        } else {
            ch.each(function () {
                jQuery(this).removeAttr('checked');
                jQuery(this).parent().removeClass('checked');
                jQuery(this).parents('tr').removeClass('warning');
            });
        }
    },
    ajax: function (url, data, callbackFun, option) {
        if (option == null || option === undefined) {
            option = $safe.v.ajaxOption;
        } else {
            if (option.method == null || option.method === undefined) {
                option.method = $safe.v.ajaxOption.method;
            }
            if (option.dataType == null || option.dataType === undefined) {
                option.dataType = $safe.v.ajaxOption.dataType;
            }
            if (option.async == null || option.async === undefined) {
                option.async = $safe.v.ajaxOption.async;
            }
        }
        jQuery.ajax({
            dataType: option.dataType,
            url: url,
            data: data,
            async: option.async,
            success: function (data) {
                callbackFun(data);
            },
            statusCode: {
                401: function () {
                },
                403: function () {
                },
                500: function () {
                }
            }
        });
    },
    turnArray: function (arr) {
        var newArr = [];
        for (var i = arr.length - 1; i >= 0; i--) {
            newArr.push(arr[i])
        }
        return newArr;
    },
    /**
     * 清理表单参数
     * @param form
     * @param option boolean类型，为true清理select插件
     */
    clearForm: function (form) {
        form.find("textarea").val("");
        form.find("input").val("");
        form.find("input[type=checkbox]").removeAttr("checked");
        form.validator("cleanUp");
    },
    dataTable: function (obj, option) {
        return obj.dataTable(option);
    }
};

$(document).on('click', '.notifyjs-foo-base .no', function () {
    $(this).trigger('notify-hide');
});
$(document).on('click', '.notifyjs-foo-base .yes', function () {
    if ($safe.v.notifyMethod != null) {
        eval("$safe.v.notifyMethod()");
    }
    $(this).trigger('notify-hide');
});

(function ($) {
    $.fn.getInputId = function (sigle) {
        var checkIds = [];

        this.each(function () {
            checkIds.push($(this).val())
        });

        if (sigle) {
            if (checkIds.length > 1) {
                $safe.notify('只能选择一条记录！', 'error');
                return false;
            }
            else if (checkIds.length === 0) {
                $safe.notify('请选择一条记录操作！', 'error');
                return false;
            }
            else {
                return checkIds[0];
            }
        } else {
            if (checkIds.length === 0) {
                $safe.notify('请选择至少一条记录操作！', 'error');
                return false;
            } else {
                return checkIds;
            }
        }
    };
})(jQuery);